﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QualityRpt.Service;
using log4net;
using System.Data;
using System.Text;
using QualityRpt.Utils;

namespace QualityRPT.CARDContract
{
    public partial class edit : System.Web.UI.Page
    {
        public string supplier_name_json = string.Empty;
        public string spec_name_json = string.Empty;
        public string spec_shortname_json = string.Empty;
        ILog logger = LogManager.GetLogger("CARDContract.Edit");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region combox列表设置
                DataTable spTable = DeviceSupplierService.GetListSupplierName(string.Empty, string.Empty, DeviceService.ICARD_DEV);
                if (spTable.Rows.Count > 0)
                {
                    StringBuilder sbText = new StringBuilder();
                    for (int i = 0; i < spTable.Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbText.Append(",");
                        }
                        sbText.Append("'" + spTable.Rows[i]["SUPPLIER_NAME"] + "'");
                    }
                    supplier_name_json = sbText.ToString();
                }
                DataTable scTable = DeviceSpecService.GetBySpIn(string.Empty, DeviceService.ICARD_DEV);
                if (scTable.Rows.Count > 0)
                {
                    StringBuilder sbScnameText = new StringBuilder();
                    StringBuilder sbScshortnameText = new StringBuilder();
                    for (int i = 0; i < scTable.Rows.Count; i++)
                    {
                        if (i > 0)
                        {
                            sbScnameText.Append(","); sbScshortnameText.Append(",");
                        }
                        sbScnameText.Append("'" + scTable.Rows[i]["SPEC_NAME"] + "'");
                        sbScshortnameText.Append("'" + scTable.Rows[i]["SPEC_SHORTNAME"] + "'");
                    }
                    spec_name_json = sbScnameText.ToString();
                    spec_shortname_json = sbScshortnameText.ToString();
                }
                #endregion
                #region 编辑加载数据
                string strcnno = Request.Params["cnno"];
                if (strcnno != null && strcnno.Trim() != string.Empty)
                {
                    DataTable detailTable = DeviceContractDetailService.GetListByCnIn(DeviceService.ICARD_DEV, strcnno,null);
                    this.repeater_num_range.DataSource = detailTable; this.repeater_num_range.DataBind();
                    if (detailTable.Rows.Count > 0)
                    {
                        DataRow eRow = detailTable.Rows[0];
                        this.h_contract_no.Value = eRow["CONTRACT_NO"].ToString();
                        this.txt_contract_no.Value = eRow["CONTRACT_NO"].ToString();
                        this.txt_contract_no.Attributes["readonly"] = "readonly";
                        this.txt_contract_name.Value = eRow["CONTRACT_NAME"].ToString();
                        this.txt_sign_date.Value = eRow["SIGNED_DATE"].ToString();
                        this.txt_supplier_name.Value = eRow["SUPPLIER_NAME"].ToString();
                        DataRow sup_row = DeviceSupplierService.GetByName(eRow["SUPPLIER_NAME"].ToString());
                        if (sup_row != null)
                        {
                            this.txt_supplier_shortname.Value = sup_row["SUPPLIER_SHORTNAME"].ToString();
                        }
                        this.txt_created_date.Value = eRow["POINT_DATE"].ToString();
                        this.txt_net_no.Value = eRow["INTEGRATION_LEVEL"].ToString();
                        this.txt_remark.Value = eRow["REMARK"].ToString();
                    }
                }
                #endregion
            }
        }
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            SaveData();
            Response.Redirect("index.aspx?pid=" + cltop.pid + "&cid=" + clleft.cid + "&nocache=" + DateTime.Now.Ticks);
        }
        protected void ButtonContinue_Click(object sender, EventArgs e)
        {
            SaveData();
            Response.Redirect("edit.aspx?pid=" + cltop.pid + "&cid=" + clleft.cid + "&nocache=" + DateTime.Now.Ticks);
        }
        private void SaveData()
        {
            string strhcnno = Request.Params["h_contract_no"];
            DataTable detailTable = DeviceContractDetailService.GetSchema();
            string strStartArray = Request.Params["start_no"];
            string strEndArray = Request.Params["end_no"];
            string strSpecArray = Request.Params["spec_name"];
            string strSpecshortArray = Request.Params["spec_shortname"];
            string strSoftversionArray = Request.Params["soft_version"];
            string strHardversionArray = Request.Params["hard_version"];
            string strKeyversionArray = Request.Params["key_version"];
            string strDevicecountArray = Request.Params["device_count"];
            string[] startArray = strStartArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] endArray = strEndArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] specnameArray = strSpecArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] specshortArray = strSpecshortArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] softversionArray = strSoftversionArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] hardversionArray = strHardversionArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] keyversionArray = strKeyversionArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] devicecountArray = strDevicecountArray.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //global param
            string strcnno = Request.Params["txt_contract_no"];
            string strcnname = Request.Params["txt_contract_name"];
            string strsuppliername = Request.Params["txt_supplier_name"];
            string strsuppliershortname = Request.Params["txt_supplier_shortname"];

            string str_net_no = Request.Params["txt_net_no"];
            string str_created_date = Request.Params["txt_created_date"];
            string str_remark = Request.Params["txt_remark"];
            //
            DataRow esupRow = DeviceSupplierService.GetByName(strsuppliername);
            if (esupRow == null)//如果没有厂商信息先存储
            {
                esupRow = DeviceSupplierService.GetSchema().NewRow();
                esupRow["SUPPLIER_NAME"] = strsuppliername;
                esupRow["SUPPLIER_SHORTNAME"] = strsuppliershortname;
                esupRow = DeviceSupplierService.Save(esupRow);
            }
            string strsigndate = Request.Params["txt_sign_date"];
            int i = 0;
            try
            {
                for (i = 0; i < startArray.Length; i++)
                {
                    DataRow curRow = detailTable.NewRow();
                    curRow["CONTRACT_NO"] = strcnno; curRow["CONTRACT_NAME"] = strcnname;
                    curRow["SIGNED_DATE"] = strsigndate; curRow["SUPPLIER_NAME"] = strsuppliername;
                    curRow["SUPPLIER_ID"] = esupRow["ID"];
                    DataRow especRow = DeviceSpecService.GetByName(specnameArray[i]);
                    if (especRow == null)
                    {
                        especRow = DeviceSpecService.GetSchema().NewRow();
                        especRow["SUPPLIER_ID"] = esupRow["ID"];
                        especRow["DEVICE_ID"] = DeviceService.ICARD_DEV;
                        especRow["SPEC_NAME"] = specnameArray[i];
                        especRow["SPEC_SHORTNAME"] = specshortArray[i];
                        especRow = DeviceSpecService.Save(especRow);
                    }
                    curRow["DEVICE_ID"] = DeviceService.ICARD_DEV;
                    curRow["SPEC_ID"] = especRow["ID"]; curRow["SPEC_NAME"] = specnameArray[i];
                    curRow["START_NO"] = startArray[i]; curRow["END_NO"] = endArray[i];
                    curRow["DEVICE_COUNT"] = devicecountArray[i];
                    curRow["SOFT_VERSION"] = softversionArray[i]; curRow["HARD_VERSION"] = hardversionArray[i];
                    curRow["KEY_VERSION"] = keyversionArray[i];
                    //global other
                    curRow["INTEGRATION_LEVEL"] = str_net_no;
                    curRow["CREATED_TIME"] = DateTime.Now;
                    curRow["POINT_DATE"] = str_created_date;
                    curRow["REMARK"] = str_remark;
                    curRow["IS_TEST"] = 0;
                    curRow["IS_CONTACT"] = 0;
                    detailTable.Rows.Add(curRow);
                }
                DeviceContractDetailService.SaveTable(strcnno, detailTable);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
    }
}